/*


 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yufei.toolinterface;

import java.awt.event.ActionEvent;

import java.awt.event.ItemEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yufei.infoExtractor.core.InfoExtractorMI;
import com.yufei.infoExtractor.core.TaskRunner;
import com.yufei.infoExtractor.core.impl.InfoExtractorM;
import com.yufei.infoExtractor.pfw.entity.InfoExtractTaskStatistics;
import com.yufei.infoExtractor.pfw.entity.Task;
import com.yufei.infoExtractor.task.InfoExtractorCommonTask;
import com.yufei.infoExtractor.util.AppUtil;
import com.yufei.infoExtractor.util.CommonUtil;
import com.yufei.infoExtractor.util.ExceptionUtil;
import com.yufei.toolinterface.listener.SwingSchedulerListener;
import com.yufei.toolinterface.listener.SwingTaskExecutorListener;
import com.yufei.toolinterface.util.ComponentFactory;
import com.yufei.toolinterface.util.DialogUtil;
import com.yufei.toolinterface.util.TableModelUtil;

/**
 *
 * @author jasstion
 */
public class TaskMPanel extends javax.swing.JFrame {
	private static Log mLog = LogFactory.getLog(InfoExtractorCommonTask.class);

	private TaskRunner taskRunner=null;
	private InfoExtractorMI infoExtractorMI=(InfoExtractorMI) AppUtil.getBeanFromBeanContainer(InfoExtractorM.class);
    public InfoExtractorMI getInfoExtractorMI() {
		return infoExtractorMI;
	}
	final static Map statusMap=new HashMap<String, Integer>();
    static{
    	/*未启动 0
    	运行中1
    	运行结束2
    	暂停3*/
    	statusMap.put("未启动", 0);
    	statusMap.put("运行中", 1);
        //运行成功
    	statusMap.put("运行结束", 2);
    	statusMap.put("暂停", 3);
    	statusMap.put("终止运行", 4);

    	statusMap.put("运行失败", 5);


    }
    JTable taskListTable = null;
    JTable taskStatusListTable=null;
    private void initTaskStatusList(){
    	List<InfoExtractTaskStatistics> tasks= new ArrayList<InfoExtractTaskStatistics>();
    	
		JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
				try {
					taskStatusListTable = ComponentFactory.createJTableForCollectionInformation(null, tasks, InfoExtractTaskStatistics.class);
			
					//disableedit
					
		} catch (Exception e) {
			// TODO Auto-generated catch block
			mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
			
		}
		jScrollPane1.setViewportView(taskStatusListTable);
		jScrollPane1.setSize(taskStatusListPanel.getWidth(), taskStatusListPanel.getHeight());
		taskStatusListPanel.add(jScrollPane1);
    }
    public String getTaskNameByTask(it.sauronsoftware.cron4j.Task currentTask){
    	return taskRunner.getTaskNameByTask(currentTask);
    }
   public void updateTaskStatusListTable(String taskName,int columnNumber,Object updateValue){
	   if(taskStatusListTable==null||taskStatusListTable.getRowCount()==0){
		   return;
	   }
	   DefaultTableModel tableModel=(DefaultTableModel) taskStatusListTable.getModel();
	   InfoExtractTaskStatistics tempInfoExtractTaskStatistics=null;
	   String tempTaskName=null;
	   for(int rowNumber=0;rowNumber<tableModel.getRowCount();rowNumber++){
		   tempTaskName=(String) tableModel.getValueAt(rowNumber, 0);
		   if(tempTaskName.equalsIgnoreCase(taskName)){
			   tableModel.setValueAt(updateValue, rowNumber, columnNumber);
			   break;
			   
		   }
	   }
	   
   }
   public void addEntryToTaskStatusListTable(InfoExtractTaskStatistics infoExtractTaskStatistics){
	   DefaultTableModel tableModel= (DefaultTableModel) taskStatusListTable.getModel();
	   String tempTaskName=null;

	   for(int rowNumber=0;rowNumber<tableModel.getRowCount();rowNumber++){
		   tempTaskName=(String) tableModel.getValueAt(rowNumber, 0);
		   if(tempTaskName.equalsIgnoreCase(infoExtractTaskStatistics.getTaskName())){
			   tableModel.removeRow(rowNumber);
			   break;
			   
		   }
	   }
       try {
			tableModel.addRow(TableModelUtil.convertEntityToVector(infoExtractTaskStatistics, tableModel));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
       
   }
    /**
     * Creates new form TaskMPanel
     */
    public TaskMPanel() {
        initComponents();
        //
        taskRunner=TaskRunner.getTaskRunnerInstance();
        taskRunner.addSchedulerListener(new SwingSchedulerListener(this));
        //注册任务相关监听器
        taskRunner.addTaskExecutorListener(new SwingTaskExecutorListener(this));
       
        //初始化taskstatus
        initTaskStatusList();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        taskListPanel = new javax.swing.JPanel();
        taskStatusListPanel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        taskStartButton = new javax.swing.JButton();
        taskStopButton = new javax.swing.JButton();
        taskKillButton = new javax.swing.JButton();
        resumeStopTaskButtoon = new javax.swing.JButton();
        taskDeleteButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        searchButton = new javax.swing.JButton();
        taskStatusSelectedBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        addTaskButton = new javax.swing.JButton();
        systemConfigButton = new javax.swing.JButton();
        taskConfigButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        taskListPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout taskListPanelLayout = new javax.swing.GroupLayout(taskListPanel);
        taskListPanel.setLayout(taskListPanelLayout);
        taskListPanelLayout.setHorizontalGroup(
            taskListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        taskListPanelLayout.setVerticalGroup(
            taskListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 286, Short.MAX_VALUE)
        );

        taskStatusListPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout taskStatusListPanelLayout = new javax.swing.GroupLayout(taskStatusListPanel);
        taskStatusListPanel.setLayout(taskStatusListPanelLayout);
        taskStatusListPanelLayout.setHorizontalGroup(
            taskStatusListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        taskStatusListPanelLayout.setVerticalGroup(
            taskStatusListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 226, Short.MAX_VALUE)
        );

        jLabel3.setText("任务运行状态信息显示");

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        taskStartButton.setText("启动任务");
        taskStartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskStartButtonActionPerformed(evt);
            }
        });
        jPanel2.add(taskStartButton);

        taskStopButton.setText("暂停任务");
        taskStopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskStopButtonActionPerformed(evt);
            }
        });
        jPanel2.add(taskStopButton);

        taskKillButton.setText("结束任务");
        taskKillButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskKillButtonActionPerformed(evt);
            }
        });
        jPanel2.add(taskKillButton);

        resumeStopTaskButtoon.setText("恢复暂停任务");
        resumeStopTaskButtoon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resumeStopTaskButtoonActionPerformed(evt);
            }
        });
        jPanel2.add(resumeStopTaskButtoon);

        taskDeleteButton.setText("删除任务");
        taskDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskDeleteButtonActionPerformed(evt);
            }
        });
        jPanel2.add(taskDeleteButton);

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        taskStatusSelectedBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "未启动", "运行中", "运行结束", "暂停", "终止运行", "运行失败" }));

        jLabel1.setText("任务状态");

        jLabel2.setText("任务显示列表");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(taskStatusSelectedBox, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(searchButton))
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(taskStatusSelectedBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2))
        );

        jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.LINE_AXIS));

        addTaskButton.setText("创建任务");
        addTaskButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTaskButtonActionPerformed(evt);
            }
        });
        jPanel4.add(addTaskButton);

        systemConfigButton.setText("系统参数设置");
        systemConfigButton.setEnabled(false);
        systemConfigButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                systemConfigButtonActionPerformed(evt);
            }
        });
        jPanel4.add(systemConfigButton);

        taskConfigButton.setText("任务参数配置");
        taskConfigButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taskConfigButtonActionPerformed(evt);
            }
        });
        jPanel4.add(taskConfigButton);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(taskListPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(taskStatusListPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 698, Short.MAX_VALUE))
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(taskListPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(taskStatusListPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 10, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private List<Task> getSelectedTask(){
      List<Task> selectedTasks=new ArrayList<Task>();
      int[] selectedRows=this.taskListTable.getSelectedRows();
 	 DefaultTableModel tableModel= (DefaultTableModel) this.taskListTable.getModel();
 	Task task=null;
 	Vector dataVector = tableModel.getDataVector();
      for(int selectedRow:selectedRows){
    	  task=(Task) TableModelUtil.convertTableRowToEntity(Task.class, tableModel, selectedRow);
          selectedTasks.add(task);
      }
      return selectedTasks;
    }
    public void selfStartBatchTasks(List<Task> tasks){
    	
    	this.taskRunner.run(tasks);
    }
    private void taskStartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskStartButtonActionPerformed
        // TODO add your handling code here:
    	//获取选中的任务列表
    	List<Task> selectedTasks=this.getSelectedTask();
    	if(CommonUtil.isEmptyOrNull(selectedTasks)){
    		DialogUtil.popUpDialog("请选择你要执行的任务！");
    		return;
    	}
    	
    	this.taskRunner.run(selectedTasks);
    	refreshTaskList();
    	
    	
    }//GEN-LAST:event_taskStartButtonActionPerformed
    //暂停任务的执行
    private void taskStopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskStopButtonActionPerformed
        // TODO add your handling code here:
    	List<Task> selectedTasks=this.getSelectedTask();
    	if(CommonUtil.isEmptyOrNull(selectedTasks)){
    		DialogUtil.popUpDialog("请选择你要暂停的任务！");
    		return;
    	}
    	/*//验证已经选择的任务的合法性（主要是状态）
    	for(Task task:selectedTasks){
    		if(!task.getStatus().equals(Task.IS_RUNNING)){
      		  DialogUtil.popUpDialog("你必须选择状态为'"+Task.IS_RUNNING+"'的任务，其他状态任务将不作处理！");		  
      	  }
    	}*/
    	String pausedResult=null;
    	for(Task needPauseTask:selectedTasks){
    		pausedResult=taskRunner.pauseTask(needPauseTask);
    		if(CommonUtil.isEmptyOrNull(pausedResult)){
    			DialogUtil.popUpDialog("暂停任务:'"+needPauseTask.getTaskName()+"'失败！继续暂停其他任务！");
    			continue;
    		}
    	}
    	refreshTaskList();

    	
    }//GEN-LAST:event_taskStopButtonActionPerformed
    //结束任务
    private void taskKillButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskKillButtonActionPerformed
        // TODO add your handling code here:
    	List<Task> selectedTasks=this.getSelectedTask();
    	if(CommonUtil.isEmptyOrNull(selectedTasks)){
    		DialogUtil.popUpDialog("请选择你要终止的任务！");
    		return;
    	}
    	/*//验证已经选择的任务的合法性（主要是状态）
    	for(Task task:selectedTasks){
    		if(!task.getStatus().equals(Task.IS_RUNNING)){
      		  DialogUtil.popUpDialog("你必须选择状态为'"+Task.IS_RUNNING+"'的任务，其他状态任务将不作处理！");		  
      	  }
    	}*/
    	String stopResult=null;
    	for(Task needStopedTask:selectedTasks){
    		stopResult=taskRunner.stopTask(needStopedTask);
    		if(CommonUtil.isEmptyOrNull(stopResult)){
    			DialogUtil.popUpDialog("终止任务:'"+needStopedTask.getTaskName()+"'失败！继续终止其他任务！");
    			continue;
    		}
        	infoExtractorMI.updateTaskStatus(Task.IS_STOPED, needStopedTask.getId());

    	}
    	refreshTaskList();

    }//GEN-LAST:event_taskKillButtonActionPerformed

    private void resumeStopTaskButtoonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resumeStopTaskButtoonActionPerformed
        // TODO add your handling code here:
    	List<Task> selectedTasks=this.getSelectedTask();
    	if(CommonUtil.isEmptyOrNull(selectedTasks)){
    		DialogUtil.popUpDialog("请选择你要继续执行的任务！");
    		return;
    	}
    	/*//验证已经选择的任务的合法性（主要是状态）
    	for(Task task:selectedTasks){
    		if(!task.getStatus().equals(Task.IS_SUSPENG)){
      		  DialogUtil.popUpDialog("你必须选择状态为'"+Task.IS_SUSPENG+"'的任务，其他状态任务将不作处理！");		  
      	  }
    	}*/
    	String stopResult=null;
    	for(Task needStopedTask:selectedTasks){
    		stopResult=taskRunner.resumeTask(needStopedTask);
    		if(CommonUtil.isEmptyOrNull(stopResult)){
    			DialogUtil.popUpDialog("继续执行暂停的任务:'"+needStopedTask.getTaskName()+"'失败！继续执行其他已经暂停的任务！");
    			continue;
    		}
    	}
    	refreshTaskList();

    }//GEN-LAST:event_resumeStopTaskButtoonActionPerformed

    private void addTaskButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTaskButtonActionPerformed
        // TODO add your handling code here:
    	  JDialog jpanel=new TaskAddDialog(this,true);
   	   
          jpanel.setLocationRelativeTo(this);
          jpanel.pack();
          jpanel.setVisible(true);

    }//GEN-LAST:event_addTaskButtonActionPerformed

    private void taskDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskDeleteButtonActionPerformed
        // TODO add your handling code here:
    	List<Task> selectedTasks=this.getSelectedTask();
    	for(Task task:selectedTasks){
        	infoExtractorMI.deleteObject(task);

    	}
    	this.searchButtonActionPerformed(null);
      	refreshTaskList();

    }//GEN-LAST:event_taskDeleteButtonActionPerformed

    private void systemConfigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_systemConfigButtonActionPerformed
        // TODO add your handling code here:
    	//JDialog jDialog=DialogUtil.CreateDialogForCustomerObj(this, true, AppUtil.getInfoExtractorConfig());
    	JDialog jDialog= new SystemConfigDialog(this, true);

    	jDialog.setLocationRelativeTo(this);
    	jDialog.pack();
    	jDialog.setVisible(true);
  
    
    }//GEN-LAST:event_systemConfigButtonActionPerformed

    private void taskConfigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taskConfigButtonActionPerformed
        // TODO add your handling code here:
    	//更改baidusearch.xml文件的某些内容
    	JDialog jDialog= new TaskConfigDialog(this, true);

    	jDialog.setLocationRelativeTo(this);
    	jDialog.pack();
    	jDialog.setVisible(true);
    	
    }//GEN-LAST:event_taskConfigButtonActionPerformed
    public void disableALlButtonRelatedWithTask(){
    	taskStartButton.setEnabled(false);
    	taskStopButton.setEnabled(false);
    	taskKillButton.setEnabled(false);
    	resumeStopTaskButtoon.setEnabled(false);
    	
    }
    public void setEnableButtonRelatedWithTask(){
    	disableALlButtonRelatedWithTask();
    	String taskStatusStr=taskStatusSelectedBox.getSelectedItem().toString();
    	int status=(Integer) statusMap.get(taskStatusStr);
    	switch (status) {
		case 0:
			taskStartButton.setEnabled(true);
			break;
case 1:
	taskStopButton.setEnabled(true);
	taskKillButton.setEnabled(true);
			break;
case 2:
	taskStartButton.setEnabled(true);

	break;
case 3:
	resumeStopTaskButtoon.setEnabled(true);
	break;
case 4:
	taskStartButton.setEnabled(true);

	break;
case 5:
	taskStartButton.setEnabled(true);

	break;
		default:
			break;
		}
    }
   public void removeTaskExecutorByTaskName(String taskName){
	   taskRunner.removeTaskExecutorByTaskName(taskName);
   }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TaskMPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TaskMPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TaskMPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TaskMPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                final TaskMPanel taskMPanel = new TaskMPanel();
                taskMPanel.addWindowListener(new WindowAdapter() {
                	public void windowClosing(WindowEvent e) {
                	int result=	JOptionPane.showConfirmDialog(taskMPanel, "您确定要关闭？如果关闭的话没有运行完毕的任务将不会保存哦！");
                       if(result==JOptionPane.OK_OPTION){                    	   
                    	   taskMPanel.destroy();
                    	   taskMPanel.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

                    	   
                       }
                       else{
                    	   taskMPanel.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

                       }
                		
                	}
				});
				taskMPanel.setVisible(true);
            }
        });
    }
    protected void destroy() {
		// TODO Auto-generated method stub
		this.taskRunner.finalize();
	}
	//任务启动的时候启动界面上对应任务的状态信息
    public void updateTaskStatusByTaskName(String taskName,Integer status){
        DefaultTableModel tableModel= (DefaultTableModel) taskListTable.getModel();
        //设置某行某列的值
        int row=0,column=0;
        int taskNameColumn=2, taskStatusColumn=3;
        int rowCount=tableModel.getRowCount();
        for(int i=0;i<rowCount;i++){
        	if(tableModel.getValueAt(i, taskNameColumn).equals(taskName)){
        		tableModel.setValueAt(String.valueOf(status), i, taskStatusColumn);
        		break;
        	}
        	
        }
       
    }
    public void refreshTaskList(){
    	this.searchButtonActionPerformed(null);
    }
    //
	private void searchButtonActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		String taskStatus=(String) taskStatusSelectedBox.getSelectedItem();
		setEnableButtonRelatedWithTask();
		if(CommonUtil.isEmptyOrNull(taskStatus)){
			DialogUtil.popUpDialog("请选择任务状态进行查询");
			return;
			
		}
		Integer status=(Integer) statusMap.get(taskStatus);
		
		List<Task> tasks=(List<Task>) infoExtractorMI.getTasksByStatus(status);
		taskListPanel.removeAll();
		JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
				try {
			taskListTable = ComponentFactory.createJTableForCollectionInformation(null, tasks, Task.class);
			//disableedit
		} catch (Exception e) {
			// TODO Auto-generated catch block
			mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
			DialogUtil.popUpDialog("任务查询ERROR！");
		}
		jScrollPane1.setViewportView(taskListTable);
		jScrollPane1.setSize(taskListPanel.getWidth(), taskListPanel.getHeight());
		taskListPanel.add(jScrollPane1);

		
	}
	public void addTaskTotaskListPanel(Task task){
		if(taskListTable==null||task.getStatus()!=statusMap.get(taskStatusSelectedBox.getSelectedItem().toString())){
			return ;
		}
		
        DefaultTableModel tableModel= (DefaultTableModel) taskListTable.getModel();
        try {
			tableModel.addRow(TableModelUtil.convertEntityToVector(task, tableModel));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void removeTaskFromTaskListPanel(int rowNumber){
        DefaultTableModel tableModel= (DefaultTableModel) taskListTable.getModel();
        try {
			tableModel.removeRow(rowNumber);//re(TableModelUtil.convertEntityToVector(task, tableModel));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addTaskButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JButton resumeStopTaskButtoon;
    private javax.swing.JButton searchButton;
    private javax.swing.JButton systemConfigButton;
    private javax.swing.JButton taskConfigButton;
    private javax.swing.JButton taskDeleteButton;
    private javax.swing.JButton taskKillButton;
    private javax.swing.JPanel taskListPanel;
    private javax.swing.JButton taskStartButton;
    private javax.swing.JPanel taskStatusListPanel;
    private javax.swing.JComboBox taskStatusSelectedBox;
    private javax.swing.JButton taskStopButton;
    // End of variables declaration//GEN-END:variables
}
